//
//  ExcelViewController.m
//  WebView加载文件
//
//  Created by xiacl on 2018/9/5.
//  Copyright © 2018年 a2345. All rights reserved.
//

#import "ExcelViewController.h"

#import <WebKit/WebKit.h>


@interface ExcelViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ExcelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%s", __func__);
    
    self.webView.scalesPageToFit = YES;
//    [self.webView setClipsToBounds:YES];//设置为界限
//    [self.webView setScalesPageToFit:YES];//页面设置为合适
    
    
   
    
    NSString *str = [[NSBundle mainBundle] pathForResource:@"章鱼星球APP-V2.0测试用例(1).xlsx" ofType:nil];
    NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString *encodedString = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
    NSURL *url = [NSURL URLWithString:encodedString];
//    [self.webView removeFromSuperview];

    
    
//    NSData *data = [NSData dataWithContentsOfFile:str];
//            [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.xlsprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];
    //
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    
    
    NSString *body = [NSString stringWithContentsOfFile:encodedString usedEncoding: nil error:nil];
    if (!body) {
        body = [NSString stringWithContentsOfFile:encodedString encoding:0x80000632 error:nil];
    }
    //还是识别不到，按GB18030编码再解码一次.
    if (!body) {
        body = [NSString stringWithContentsOfFile:encodedString encoding:0x80000631 error:nil];
    }
//    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//    self.webView.delegate = self;
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKWebView *wkWebVeiw = [[WKWebView alloc]  initWithFrame:self.view.bounds configuration:config];
//    wkWebVeiw.UIDelegate = self;
    [self.view addSubview:wkWebVeiw];
    wkWebVeiw.backgroundColor = [UIColor greenColor];
    [wkWebVeiw loadRequest:[NSURLRequest requestWithURL:url]];
    
//    NSString *mimetType = [self mimeType:url];
//    NSLog(@"%@", mimetType);
//    [wkWebVeiw loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.execlprocessingml.document" characterEncodingName:@"UTF-8" baseURL:nil];
    
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"%s", __func__);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"%s", __func__);
    
//    webView.scalesPageToFit = YES;
    [webView.scrollView sizeToFit];

    CGFloat webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]floatValue];
    
//    webView.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, webViewHeight);
    NSLog(@"%llf", webViewHeight);
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"%s", __func__);
}


- (NSString *)mimeType:(NSURL *)url
{
    //1NSURLRequest
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //2NSURLConnection
    
    //3 在NSURLResponse里，服务器告诉浏览器用什么方式打开文件。
    
    //使用同步方法后去MIMEType
    NSURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    return response.MIMEType;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
